iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 20
0
自我挑戰組

30天教你學會Git系列 第 20

[Day20] 你要Push你要先講啊!!

  • 分享至 

  • xImage
  •  
  • 好,我們常常在Push的時候,你會遇到很多問題啊!
  • 舉個例子:
    • 你跟你的同學正在使用同個Repo
    • 你的同學昨天晚上在你睡覺之後,又commit了一次,並且push了
    • 所以在你電腦上的那一份是舊的版本,但你不知道
    • 這時候你想要Push的話,你就會得到一串錯誤訊息
$ git push
To https://github.com/jackey10055206/test.git
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'https://github.com/jackey10055206/test.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

$ git pull
remote: Enumerating objects: 6, done.
remote: Counting objects: 100% (6/6), done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 4 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (4/4), done.
From https://github.com/jackey10055206/test
   3bd6182..9d0d18f  master     -> origin/master
Merge made by the 'recursive' strategy.
 123.txt | 1 +
 1 file changed, 1 insertion(+)
 
 $ git push
Enumerating objects: 9, done.
Counting objects: 100% (8/8), done.
Delta compression using up to 8 threads
Compressing objects: 100% (4/4), done.
Writing objects: 100% (5/5), 609 bytes | 609.00 KiB/s, done.
Total 5 (delta 0), reused 0 (delta 0)
To https://github.com/jackey10055206/test.git
   9d0d18f..f77fa62  master -> master
  • 沒錯,他會跟你說,你需要先使用git pull 你才能執行git push

  • 所以我們上面介紹了一種"先拉在推"的方法嘛!

  • 接下來我在跟你說一種"我是老大"的方法

    • 一模一樣的情況,只不過這一次你發現他寫的東西是錯的
    • 你剛好想要把你同學新增的東西給抹掉
    • 這時候你就可以使用這個秘密武器git push -f
$ git push
To https://github.com/jackey10055206/test.git
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'https://github.com/jackey10055206/test.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

$ git push -f
Enumerating objects: 27, done.
Counting objects: 100% (27/27), done.
Delta compression using up to 8 threads
Compressing objects: 100% (17/17), done.
Writing objects: 100% (27/27), 4.05 KiB | 1.35 MiB/s, done.
Total 27 (delta 0), reused 0 (delta 0)
To https://github.com/jackey10055206/test.git
 + b1b9f61...2e1efe4 master -> master (forced update)
  • 在這裡,後面的參數-f 意思就是強制
  • 我們強制要push我們手中的版本上去!

明天來講講git clone吧!


上一篇
[Day19] 昨天聊了推的概念,今天來.....拉?
下一篇
[Day21] 我怎麼看人家都會在那邊clone來clone去?!
系列文
30天教你學會Git30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言